<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/3/4
 * Time: 16:19
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Task extends Base
{
    /**
     * 任务列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $task_title = input('task_title');
            $task_type = input('task_type');
            $where['task_status'] = 1;
            // 标题
            if(isset($task_title) && $task_title != ''){
                $where['task_title'] = ['like', "%$task_title%"];
            }
            // 类型
            if(isset($task_type) && $task_type != ''){
                $where['task_type'] = $task_type;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('task')->where($where)->count();// 获取总条数
            $lists = Db::name('task')
                ->where($where)
                ->field('task_id, task_title, task_district_names, task_type, task_share_number, 
                task_time')
                ->order('task_time desc')
                ->page($Nowpage, $limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        
        return $this->fetch();
    }
    
    /**
     * 发布任务
     * @return mixed|\think\response\Json
     */
    public function taskAdd(){
        if(Request::instance()->isPost()){
            $task_title = trim(input('task_title'), ' ');
            $task_district_ids = input('task_district_ids');
            $task_type = input('task_type'); // 1 文字图集  2 文字视频  3 文字链接
            $task_content = input('task_content');
            $task_images = input('task_images');
            $task_video = input('task_video');
            $task_link = input('task_link');
            $task_district_name = Db::table('up_district')
                ->where('dis_id', 'IN', $task_district_ids)->column('dis_name');
            $insert = [
                'task_title' => $task_title,
                'task_district_ids' => $task_district_ids,
                'task_district_names' => implode(',', $task_district_name),
                'task_type' => $task_type,
                'task_content' => $task_content,
                'task_time' => date('Y-m-d H:i:s')
            ];
            if($task_type == 1){
                if(empty($task_images)){
                    return json(['code' => 100, 'msg' => '请上传图集']);
                }else{
                    $insert['task_images'] = $task_images;
                }
            }elseif($task_type == 2){
                if(empty($task_video)){
                    return json(['code' => 100, 'msg' => '请上传视频']);
                }else{
                    // 转码
                    $this->zhuanma($task_video);
                    $insert['task_video'] = config('qiniu.domain') . 'video_' . $task_video;
                }
            }else{
               if(empty($task_link)){
                   return json(['code' => 100, 'msg' => '请填写链接']);
               }else{
                   $insert['task_link'] = $task_link;
               }
            }
            // 区域内的店长
            $shopkeeper = Db::table('up_u_user')
                ->where('user_is_shopkeeper', 1)
                ->where('user_status', 1)
                ->where('user_district_id', 'IN', $task_district_ids)
                ->column('user_id');
            $task_shopkeeper = [];
            
            Db::startTrans();
            try{
                // 任务表
                $task_id = Db::table('up_task')->insertGetId($insert);
                // 店长任务
                if($shopkeeper){
                    foreach($shopkeeper as $k => $v){
                        $task_shopkeeper[$k] = [
                            'tu_user_id' => $v,
                            'tu_task_id' => $task_id,
                            'tu_user_type' => 1,
                            'tu_time' => date('Y-m-d H:i:s')
                        ];
                    }
                    Db::table('up_task_user')->insertAll($task_shopkeeper);
                }
                
                Db::commit();
                $result = ['code' => 1, 'msg' => '发布成功'];
            }catch(\Exception $e){
                Db::rollback();
                $result = ['code' => 2, 'msg' => $e->getMessage()];
            }
            if($result['code'] == 1){
                // 推送
                $list = [];
                foreach($shopkeeper as $k => $v){
                    array_push($list, (string)$v);
                }
                try{
                    $this->Jpush(1, $list, '您有一个新任务', ['send_type' => 1, 'task_id' => $task_id]);
                }catch(\Exception $e){
        
                }
                
                return json(['code' => 200, 'msg' => $result['msg']]);
            }else{
                return json(['code' => 100, 'msg' => $result['msg']]);
            }
        }
        $district = Db::table('up_district')->field('dis_id, dis_name')->select();
        return $this->fetch('task/taskadd', ['district' => $district]);
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function taskDetail(){
        $task_id = input('task_id');
        $task = Db::table('up_task')
            ->where('task_id', $task_id)
            ->field('task_title, task_district_names, task_type, task_content, task_images, 
            task_video, task_link, task_share_number, task_time')
            ->find();
        // 已完成用户
        $finish_user = Db::table('up_task_user')
            ->join('up_u_user', 'user_id = tu_user_id', 'LEFT')
            ->where('tu_task_id', $task_id)
            ->where('tu_user_is_commit', 2)
            ->column('user_username');
        if($finish_user){
            $task['finish_user'] = implode(',', $finish_user);
        }else{
            $task['finish_user'] = '';
        }
        // 图集
        if($task['task_images']){
            $task['task_images'] = explode(',', $task['task_images']);
        }else{
            $task['task_images'] = [];
        }
        return $this->fetch('task/taskdetail', ['task' => $task]);
    }
    
    /**
     * 任务用户
     * @return mixed|\think\response\Json
     */
    public function taskUser(){
        if(Request::instance()->isAjax()){
            $user_name = input('user_name');
            $tu_user_is_commit = input('tu_user_is_commit');
            $tu_admin_is_check = input('tu_admin_is_check');
            $where['tu_task_id'] = session('task_id');
            
            if(isset($user_name) && $user_name != ''){
                $where['user_username'] = ['like', "%$user_name%"];
            }
            if(isset($tu_user_is_commit) && $tu_user_is_commit != ''){
                $where['tu_user_is_commit'] = $tu_user_is_commit;
            }
            if(isset($tu_admin_is_check) && $tu_admin_is_check != ''){
                $where['tu_admin_is_check'] = $tu_admin_is_check;
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('task_user')
                ->join('up_u_user', 'user_id = tu_user_id' , 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('task_user')
                ->join('up_u_user', 'user_id = tu_user_id' , 'LEFT')
                ->where($where)
                ->field('tu_id, tu_user_id, tu_user_type, tu_user_is_commit, tu_admin_is_check, tu_time, 
                tu_user_commit_time, tu_admin_check_time, user_username')
                ->order('tu_user_is_commit desc, tu_admin_is_check asc, tu_time desc')
                ->page($Nowpage, $limits)
                ->select();
    
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $task_id = input('task_id');
        if(isset($task_id) && $task_id != ''){
            session('task_id', $task_id);
        }
        return $this->fetch('task/taskuser');
    }
    
    /**
     * 删除成功
     * @return \think\response\Json
     */
    public function taskDel(){
        $task_id = input('id');
        $res = Db::table('up_task')->where('task_id', $task_id)->setField('task_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 完成情况
     * @return mixed|\think\response\Json
     */
    public function taskFinish(){
        if(Request::instance()->isAjax()){
            $where['bf_tu_id'] = session('tu_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('task_finish')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('task_finish')
                ->where($where)
                ->field('bf_image, bf_time')
                ->order('bf_time desc')
                ->page($Nowpage, $limits)
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    if($v['bf_image']){
                        $lists[$k]['bf_image'] = explode(',',$v['bf_image']);
                    }
                }
            }
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $tu_id = input('tu_id');
        if(isset($tu_id) && $tu_id != ''){
            session('tu_id', $tu_id);
        }
        return $this->fetch('task/taskfinish');
    }
    
    /**
     * 审核
     * @return \think\response\Json
     */
    public function taskCheck(){
        $tu_id = input('id');
        $res = Db::table('up_task_user')->where('tu_id', $tu_id)
            ->update(['tu_admin_is_check' => 2, 'tu_admin_check_time' => date('Y-m-d H:i:s')]);
        if($res){
            return json(['code' => 200, 'msg' => '审核成功']);
        }else{
            return json(['code' => 100, 'msg' => '审核失败']);
        }
    }
}